import { reactive } from 'vue'

// Toast状态管理
const toastState = reactive({
  toasts: []
})

let toastId = 0

// 创建Toast
const createToast = (options) => {
  const id = ++toastId
  const toast = {
    id,
    type: options.type || 'info',
    title: options.title || '',
    message: options.message || '',
    duration: options.duration !== undefined ? options.duration : 3000,
    autoClose: options.autoClose !== undefined ? options.autoClose : true
  }
  
  toastState.toasts.push(toast)
  
  return id
}

// 移除Toast
const removeToast = (id) => {
  const index = toastState.toasts.findIndex(toast => toast.id === id)
  if (index > -1) {
    toastState.toasts.splice(index, 1)
  }
}

// 清除所有Toast
const clearToasts = () => {
  toastState.toasts.splice(0)
}

// 便捷方法
const toast = {
  success: (message, options = {}) => {
    return createToast({ ...options, type: 'success', message })
  },
  
  error: (message, options = {}) => {
    return createToast({ ...options, type: 'error', message })
  },
  
  warning: (message, options = {}) => {
    return createToast({ ...options, type: 'warning', message })
  },
  
  info: (message, options = {}) => {
    return createToast({ ...options, type: 'info', message })
  },
  
  // 自定义Toast
  show: (options) => {
    return createToast(options)
  },
  
  // 移除指定Toast
  remove: removeToast,
  
  // 清除所有Toast
  clear: clearToasts
}

export { toastState, toast }
